1 /****************************** Module Header ******************************\
2 Module Name: Wallpaper.cpp
3 Project: CppSetDesktopWallpaper
4 Copyright (c) Microsoft Corporation.
6 The file defines the wallpaper helper functions.
8 BOOL SupportJpgAsWallpaper();
9 BOOL SupportFitFillWallpaperStyles();
10 HRESULT SetDesktopWallpaper(PWSTR pszFile, WallpaperStyle style);
12 SetDesktopWallpaper is the key function that sets the desktop wallpaper. The
13 function body is composed of configuring the wallpaper style in the registry
14 and setting the wallpaper with SystemParametersInfo.
16 This source is subject to the Microsoft Public License.
17 See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
18 All other rights reserved.
20 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
21 EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
23 \***************************************************************************/
27 #include "Wallpaper.h"
31 // FUNCTION: SupportJpgAsWallpaper()
33 // PURPOSE: Determine if .jpg files are supported as wallpaper in the
34 // current operating system. The .jpg wallpapers are not supported before
37 BOOL
SupportJpgAsWallpaper()
39 OSVERSIONINFOEX osVersionInfoToCompare
= { sizeof(OSVERSIONINFOEX
) };
40 osVersionInfoToCompare
.dwMajorVersion
= 6;
41 osVersionInfoToCompare
.dwMinorVersion
= 0;
43 ULONGLONG comparisonInfo
= 0;
44 BYTE conditionMask
= VER_GREATER_EQUAL
;
45 VER_SET_CONDITION(comparisonInfo
, VER_MAJORVERSION
, conditionMask
);
46 VER_SET_CONDITION(comparisonInfo
, VER_MINORVERSION
, conditionMask
);
48 return VerifyVersionInfo(&osVersionInfoToCompare
,
49 VER_MAJORVERSION
| VER_MINORVERSION
, comparisonInfo
);
54 // FUNCTION: SupportFitFillWallpaperStyles()
56 // PURPOSE: Determine if the fit and fill wallpaper styles are supported in
57 // the current operating system. The styles are not supported before
60 BOOL
SupportFitFillWallpaperStyles()
62 OSVERSIONINFOEX osVersionInfoToCompare
= { sizeof(OSVERSIONINFOEX
) };
63 osVersionInfoToCompare
.dwMajorVersion
= 6;
64 osVersionInfoToCompare
.dwMinorVersion
= 1;
66 ULONGLONG comparisonInfo
= 0;
67 BYTE conditionMask
= VER_GREATER_EQUAL
;
68 VER_SET_CONDITION(comparisonInfo
, VER_MAJORVERSION
, conditionMask
);
69 VER_SET_CONDITION(comparisonInfo
, VER_MINORVERSION
, conditionMask
);
71 return VerifyVersionInfo(&osVersionInfoToCompare
,
72 VER_MAJORVERSION
| VER_MINORVERSION
, comparisonInfo
);
77 // FUNCTION: SetDesktopWallpaper(PCWSTR, WallpaperStyle)
79 // PURPOSE: Set the desktop wallpaper.
82 // * pszFile - Path of the wallpaper
83 // * style - Wallpaper style
85 HRESULT
SetDesktopWallpaper(PWSTR pszFile
, WallpaperStyle style
)
89 // Set the wallpaper style and tile.
90 // Two registry values are set in the Control Panel\Desktop key.
92 // 0: The wallpaper picture should not be tiled
93 // 1: The wallpaper picture should be tiled
95 // 0: The image is centered if TileWallpaper=0 or tiled if TileWallpaper=1
96 // 2: The image is stretched to fill the screen
97 // 6: The image is resized to fit the screen while maintaining the aspect
98 // ratio. (Windows 7 and later)
99 // 10: The image is resized and cropped to fill the screen while
100 // maintaining the aspect ratio. (Windows 7 and later)
102 // Open the HKCU\Control Panel\Desktop registry key.
104 hr
= HRESULT_FROM_WIN32(RegOpenKeyEx(HKEY_CURRENT_USER
,
105 L
"Control Panel\\Desktop", 0, KEY_READ
| KEY_WRITE
, &hKey
));
108 PWSTR pszWallpaperStyle
;
109 PWSTR pszTileWallpaper
;
114 pszWallpaperStyle
= L
"0";
115 pszTileWallpaper
= L
"1";
119 pszWallpaperStyle
= L
"0";
120 pszTileWallpaper
= L
"0";
124 pszWallpaperStyle
= L
"2";
125 pszTileWallpaper
= L
"0";
128 case Fit
: // (Windows 7 and later)
129 pszWallpaperStyle
= L
"6";
130 pszTileWallpaper
= L
"0";
133 case Fill
: // (Windows 7 and later)
134 pszWallpaperStyle
= L
"10";
135 pszTileWallpaper
= L
"0";
139 // Set the WallpaperStyle and TileWallpaper registry values.
140 DWORD cbData
= lstrlen(pszWallpaperStyle
) * sizeof(*pszWallpaperStyle
);
141 hr
= HRESULT_FROM_WIN32(RegSetValueEx(hKey
, L
"WallpaperStyle", 0, REG_SZ
,
142 reinterpret_cast<const BYTE
*>(pszWallpaperStyle
), cbData
));
145 cbData
= lstrlen(pszTileWallpaper
) * sizeof(*pszTileWallpaper
);
146 hr
= HRESULT_FROM_WIN32(RegSetValueEx(hKey
, L
"TileWallpaper", 0, REG_SZ
,
147 reinterpret_cast<const BYTE
*>(pszTileWallpaper
), cbData
));
153 // Set the desktop wallpapaer by calling the Win32 API SystemParametersInfo
154 // with the SPI_SETDESKWALLPAPER desktop parameter. The changes should
155 // persist, and also be immediately visible.
158 if (!SystemParametersInfo(SPI_SETDESKWALLPAPER
, 0,
159 static_cast<PVOID
>(pszFile
),
160 SPIF_UPDATEINIFILE
| SPIF_SENDWININICHANGE
))
162 hr
= HRESULT_FROM_WIN32(GetLastError());